Skip to content

Conversation

@slobodanadamovic
Copy link
Contributor

@slobodanadamovic slobodanadamovic commented Jun 10, 2025

A followup to #128440, which introduces a new managed_by field (<1>) that will be returned in the response of the Authenticate API.

Besides managed_by field, it also captures additional internal field (<2>) for cloud API key authentication and exposes it as part of the api_key fields.

{
  "username": "omSAd5YBK3gZiBcD-GvX", 
  "roles": [ "viewer" ],
  "metadata": {
    ...
  },
  "enabled": true,
  "authentication_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "lookup_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "authentication_type": "api_key",
  "api_key": { 
    "id": "omSAd5YBK3gZiBcD-GvX",
    "name": "my cloud API key",
    "managed_by": "cloud", <1>
    "internal": false <2>
  }
}
  • Additionally it implements the Authentication#canAccessResourcesOf for the cloud API keys. Ownership check allows access only to the same cloud API key.

  • And lastly, adds a consistency check for cloud API keys in Authentication#checkConsistencyForApiKeyAuthenticationType.

@elasticsearchmachine elasticsearchmachine added v9.1.0 serverless-linked Added by automation, don't add manually labels Jun 10, 2025
slobodanadamovic and others added 8 commits June 11, 2025 09:10
…hentication-metadata-and-validation

# Conflicts:
#	server/src/main/java/org/elasticsearch/TransportVersions.java
…hentication-metadata-and-validation

# Conflicts:
#	server/src/main/java/org/elasticsearch/TransportVersions.java
#	x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java
#	x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationField.java
#	x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationSerializationTests.java
#	x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java
@slobodanadamovic slobodanadamovic marked this pull request as ready for review June 17, 2025 22:01
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same on this PR: looks good, but I want to take a bit of time in the morning to fully complete the review. Don't foresee big change requests, nice work 👍

A couple suggestions.

}
checkConsistencyForApiKeyAuthenticatingSubject("API key");
final String prefixMessage = authenticatingRealm.isCloudApiKeyRealm() ? "Cloud API key" : "API key";
checkConsistencyForApiKeyAuthenticatingSubject(prefixMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer a separate method checkConsistencyForCloudApiKeyAuthenticatingSubject for cloud API keys here -- cloud API keys are fundamentally different from stack API keys. For example, we don't expect role descriptors in cloud API key metadata. We currently don't check for the presence of role descriptors for stack API keys either but could easily do that in the future. Similarly I think it's a good consistency check that cloud API keys don't have role descriptors in metadata.

I'd also add a check to checkConsistencyForApiKeyAuthenticatingSubject that internal is not set since that doesn't make sense for stack API keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ Agreed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed the changes which add dedicated consistency check for cloud API keys and additionally made run-as unsupported for cloud API keys. Currently, there is no need to support them. We can always enable it in the future.

public static final String API_KEY_CREATOR_REALM_TYPE = "_security_api_key_creator_realm_type";
public static final String API_KEY_ID_KEY = "_security_api_key_id";
public static final String API_KEY_NAME_KEY = "_security_api_key_name";
public static final String API_KEY_MANAGED_BY_KEY = "_security_api_key_managed_by";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this metadata field -- we can always infer the value based on the subject type. Less is more in this case, IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. FWIW I initially started by inferring it, then realized we planned to have it for all authentication types. Since we are not requring it for all, just API keys (at the moment), I'll change this and remove it.

Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed on Zoom: slight leaning towards not storing api key ID in metadata since it enforces that the API key ID is the principal for cloud API keys and achieves consistency by definition instead of requiring explicit consistency checks (API key ID == principal for cloud API keys).

Looks good otherwise!

@slobodanadamovic slobodanadamovic requested a review from n1v0lg June 23, 2025 07:33
Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@slobodanadamovic slobodanadamovic merged commit 2502a36 into elastic:main Jun 23, 2025
39 checks passed
kderusso pushed a commit to kderusso/elasticsearch that referenced this pull request Jun 23, 2025
…#129227)

A followup to elastic#128440, which introduces a new `managed_by` field (`<1>`) that will be returned in the response of the Authenticate API.


Besides `managed_by` field, it also captures additional `internal` field (`<2>`) for cloud API key authentication and exposes it as part of the `api_key` fields.

```json
{
  "username": "omSAd5YBK3gZiBcD-GvX", 
  "roles": [ "viewer" ],
  "metadata": {
    ...
  },
  "enabled": true,
  "authentication_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "lookup_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "authentication_type": "api_key",
  "api_key": { 
    "id": "omSAd5YBK3gZiBcD-GvX",
    "name": "my cloud API key",
    "managed_by": "cloud", <1>
    "internal": false <2>
  }
}

```


- Additionally it implements the `Authentication#canAccessResourcesOf` for the cloud API keys. Ownership check allows access only to the same cloud API key.

- And lastly, adds a consistency check for cloud API keys in `Authentication#checkConsistencyForApiKeyAuthenticationType`.
julian-elastic pushed a commit to julian-elastic/elasticsearch that referenced this pull request Jun 24, 2025
…#129227)

A followup to elastic#128440, which introduces a new `managed_by` field (`<1>`) that will be returned in the response of the Authenticate API.


Besides `managed_by` field, it also captures additional `internal` field (`<2>`) for cloud API key authentication and exposes it as part of the `api_key` fields.

```json
{
  "username": "omSAd5YBK3gZiBcD-GvX", 
  "roles": [ "viewer" ],
  "metadata": {
    ...
  },
  "enabled": true,
  "authentication_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "lookup_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "authentication_type": "api_key",
  "api_key": { 
    "id": "omSAd5YBK3gZiBcD-GvX",
    "name": "my cloud API key",
    "managed_by": "cloud", <1>
    "internal": false <2>
  }
}

```


- Additionally it implements the `Authentication#canAccessResourcesOf` for the cloud API keys. Ownership check allows access only to the same cloud API key.

- And lastly, adds a consistency check for cloud API keys in `Authentication#checkConsistencyForApiKeyAuthenticationType`.
mridula-s109 pushed a commit to mridula-s109/elasticsearch that referenced this pull request Jun 25, 2025
…#129227)

A followup to elastic#128440, which introduces a new `managed_by` field (`<1>`) that will be returned in the response of the Authenticate API.


Besides `managed_by` field, it also captures additional `internal` field (`<2>`) for cloud API key authentication and exposes it as part of the `api_key` fields.

```json
{
  "username": "omSAd5YBK3gZiBcD-GvX", 
  "roles": [ "viewer" ],
  "metadata": {
    ...
  },
  "enabled": true,
  "authentication_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "lookup_realm": {
    "name": "_cloud_api_key",
    "type": "_cloud_api_key"
  },
  "authentication_type": "api_key",
  "api_key": { 
    "id": "omSAd5YBK3gZiBcD-GvX",
    "name": "my cloud API key",
    "managed_by": "cloud", <1>
    "internal": false <2>
  }
}

```


- Additionally it implements the `Authentication#canAccessResourcesOf` for the cloud API keys. Ownership check allows access only to the same cloud API key.

- And lastly, adds a consistency check for cloud API keys in `Authentication#checkConsistencyForApiKeyAuthenticationType`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>non-issue :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) serverless-linked Added by automation, don't add manually Team:Security Meta label for security team v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants